//
//  XRDataBaseTool.m
//  DiyArt
//
//  Created by dllo on 15/9/12.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "XRDataBaseTool.h"

@implementation XRDataBaseTool

+ (XRDataBaseTool *)shareXRDataBaseTool{
    
    static XRDataBaseTool *dataBaseTool = nil;
    static dispatch_once_t oneToKen;
    dispatch_once(&oneToKen, ^{
        
        dataBaseTool = [[XRDataBaseTool alloc] init];
        [dataBaseTool OpenDB];
        [dataBaseTool createCourseTable];
    });
    
    return dataBaseTool;
}

// ------- 打开数据库 --------//
- (void)OpenDB{
    
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];
    
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Collect.sqlite"];
    NSLog(@"%@",documentPath);
    int result = sqlite3_open([documentPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else{
        NSLog(@"数据库打开失败");
    }
}

// --------- 创建一个收藏的表格 -------------//
- (void)createCourseTable
{
    
    NSString *sqliteStr = @"create table if not exists course (albumId integer primary key,id integer, coverLarge text, title text, playsCounts integer, tracks integer)";

    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"收藏表格创建成功");
    }else{
        NSLog(@"收藏表格创建失败");
    }
}



// ------- 判断是否保存 ---------------------//


- (XRIsSaveDataInTable)isSaveDataInTableWithAlbumId:(NSInteger)albumId
{

    NSString *sqliteStr = [NSString stringWithFormat:@"select * from course where albumId = '%ld'",albumId];

    // 跟随指针
    sqlite3_stmt *stmt = nil;

    int result = sqlite3_prepare_v2(dbPoint, [sqliteStr UTF8String], -1, &stmt, nil);

    if (result == SQLITE_OK) {
        NSLog(@"查询教程表格成功");
        
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            NSLog(@"教程已在表中");
            return XRInTabel;
        }
            sqlite3_finalize(stmt);
            NSLog(@"教程没有在表中");
            return XRNotInTable;
    }else{
        sqlite3_finalize(stmt);
        NSLog(@"查询教程表格失败啊!");
        return XRSelectError;
    }
}

 // ------------- 添加 收藏 ---------------//
- (void)insertDataWithCourseModel:(ALPS_RightTableCellModel *)model
{
    
    NSString *sqliteStr =[NSString stringWithFormat:@"insert into course (albumId, id, coverLarge, title, playsCounts, tracks) values('%ld','%ld','%@', '%@', '%ld', '%ld')", model.albumId, model.id, model.coverLarge, model.title, model.playsCounts, model.tracks];
    
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"添加教程成功");
    }else{
        NSLog(@"添加教程失败");
    }
  
}



// ------------------  删除 --------------------//
- (void)delegateDataWithAlbumId:(NSInteger)albumId
{

    NSString *sqlStr = [NSString stringWithFormat:@"delete from course where albumId = '%ld'",albumId];

    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除教程成功!");
    }else{
        NSLog(@"删除教程失败!");
    }

}

//// ------------------- 更新 --------------------//
//- (void)upDataWithHand_id:(NSString *)hand_id Num:(NSString *)num type:(XRDataType)type
//{
//    switch (type) {
//        case XRCollect:
//        {
//            NSString *sqlStr = [NSString stringWithFormat:@"update course set collect= '%@' where hand_id = '%@'",num, hand_id];
//            int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
//            if (result == SQLITE_OK) {
//                NSLog(@"更新jiaocheng成功");
//            }else{
//                NSLog(@"更新jiaocheng失败! 原因:%d",result);
//            }
//  
//        }
//            break;
//        case XRLaud:
//        {
//            
//            NSString *sqlStr = [NSString stringWithFormat:@"update laud set laud = '%@' where hand_id = '%@'",num, hand_id];
//            int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
//            if (result == SQLITE_OK) {
//                NSLog(@"zan数字更新成功");
//            }else{
//                NSLog(@"zan数字更新失败! 原因:%d",result);
//            }
//        }
//            break;
//            
//        default:
//            break;
//    }
//
//}

// -------------- 遍历所有取出 -------------------//
- (NSMutableArray *)selectAll
{
    
   NSString *sqlStr = @"select * from course";
    
    NSMutableArray *courseArr = [NSMutableArray array];
    
     sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);

    if (result == SQLITE_OK) {
        NSLog(@"查询收藏所有表格成功!");
      while (sqlite3_step(stmt) == SQLITE_ROW) {
          
          NSInteger albumId = sqlite3_column_int64(stmt, 0);
          NSInteger id = sqlite3_column_int64(stmt, 1);
          const unsigned char *coverLarge = sqlite3_column_text(stmt, 2);
          const unsigned char *title = sqlite3_column_text(stmt, 3);
          NSInteger playsCounts = sqlite3_column_int64(stmt, 4);
          NSInteger tracks = sqlite3_column_int64(stmt, 5);

          NSInteger coursealbumId = albumId;
          NSInteger courseid = id;
          NSString *coursecoverLarge = [NSString stringWithUTF8String:(const char *)coverLarge];
          NSString *coursetitle = [NSString stringWithUTF8String:(const char *)title];
          NSInteger courseplaysCounts = playsCounts;
          NSInteger coursetracks = tracks;
          
            
            ALPS_RightTableCellModel *model = [[ALPS_RightTableCellModel alloc] init];
            model.albumId = coursealbumId;
            model.id = courseid;
            model.coverLarge = coursecoverLarge;
            model.title = coursetitle;
            model.playsCounts = courseplaysCounts;
            model.tracks = coursetracks;

            [courseArr addObject:model];
        }

    }else{
        sqlite3_finalize(stmt);
        NSLog(@"查询教程失败");
    }
    sqlite3_finalize(stmt);
    return courseArr;
}

- (void)deleteAllCourse
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from course"];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除全部教程成功!");
    }else{
        NSLog(@"删除全部教程失败!");
    }
}

@end
